import db from '../../db/inedx.js'
import { crawlWeb } from '../../../../utils/crawler.js'
import { saveJson, sleep, random, readJson } from '../../../../utils/util.js'
import { isDev, dataDir, techniqueDir, subTechniqueDir } from '../../config/index.js'

import { glob } from 'glob'

import Page from '../../page.js'

class SubTechnique {
  constructor() {
    this.page = new Page()
  }

  get db() {
    return db.projectConfig
  }

  get config() {
    return db.projectConfig.data
  }

  // 开始任务
  async start() {
    await this.crawList()
  }

  // 抓取列表
  async crawList() {
    // var datasources = []
    // $('table.table-bordered tbody tr').each(function () {
    //   var row = $(this)
    //   var id = row.find('td:eq(0) a').text().trim()
    //   var name = row.find('td:eq(1) a').text().trim()
    //   var url = 'https://attack.mitre.org' + row.find('td:eq(1) a').attr('href')
    //   var domain = row.find('td:eq(2)').text().trim()
    //   var description = row.find('td:eq(3)').text().trim()
    //   datasources.push({
    //     id: id,
    //     name,
    //     domain,
    //     url,
    //     description: description,
    //   })
    // })
    // console.log(datasources)

    const datasources = await readJson(`${dataDir}/datasource.json`)
    for (let i = 0; i < datasources.length; i++) {
      const datasource = datasources[i]
      // const { html } = await crawlWeb(datasource.url)
      await this.crawlContent(datasource.url, datasource)
    }
  }

  // 抓取内容
  async crawlContent(url, item = {}) {
    await sleep(random(5000, 10000)) // 延迟1-5秒执行
    const { $ } = await crawlWeb(url)
    const json = this.getContentJson($)
    const data = Object.assign(item, json)

    saveJson(`${dataDir}/datasource/${data.id}.json`, data)

    // this.config.subTechniqueContentIndex += 1
    // await this.db.write()
  }

  getContentJson($) {
    // Extract main title
    var title = $('h1').text().trim()

    // Extract description
    var content = $('.description-body').html().trim()

    // Extract metadata from card
    var metadata = {}

    $('.col-md-4 .card-data').each(function () {
      var [key, value] = $(this).find('.col-11').text().trim().split(':')

      metadata[key.trim()] = value.trim()
    })

    const dataComponents = []

    $('.section-view').each(function () {
      // Extract techniques
      var techniques = []
      let techniqueId = ''
      let domain = ''
      $(this)
        .find('.techniques-used tbody tr')
        .each(function () {
          // Skip sub-technique rows (they have empty first cell)
          const tdCount = $(this).find('td').length

          let subTechniqueId = ''

          techniqueId = $(this).find('td:eq(1) a').text() || techniqueId
          domain = $(this).find('td:eq(0)').text().trim() || domain

          if (tdCount === 5) {
            subTechniqueId = $(this).find('td:eq(2) a').text()
          }

          var technique = {
            domain,
            // id: $(this).find('td:eq(1) a').attr('href') || $(this).find('td:eq(2) a').attr('href'),
            id: techniqueId + subTechniqueId,
            name: $(this).find('td:eq(3)').text().trim(),
            use: $(this).find('td:eq(4)').text().trim(),
          }
          techniques.push(technique)
        })

      dataComponents.push({
        title: $(this).find('h4').text().trim(),
        content: $(this).find('.description-body').html().trim(),
        techniques,
      })
    })

    // Extract references
    var references = []
    $('.scite-citation a').each(function () {
      references.push({
        id: $(this).attr('name'),
        text: $(this).text().trim(),
        url: $(this).attr('href'),
      })
    })

    // Combine all extracted data
    var extractedData = {
      title,
      content,
      metadata,
      dataComponents,
      references,
    }

    // console.log(extractedData)

    return extractedData
  }
}

new SubTechnique().start()
// new SubTechnique().crawlContent('https://attack.mitre.org/mitigations/M1049')

export default SubTechnique
